package com.pq.controller;

import com.pq.pojo.order.OrderCart;
import com.pq.pojo.order.OrderTable;
import com.pq.pojo.user.User;
import com.pq.service.order.OrderCartService;
import com.pq.service.order.OrderTableService;
import com.pq.utils.CookieUtil;
import com.pq.utils.ObjectMapperUtil;
import com.pq.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class OrderTableController {
    private static final String PQ_TICKET = "PQ_TICKET";

    @Autowired
    private Jedis jedis;

    @Autowired
    private OrderTableService orderTableService;
    @Autowired
    private OrderCartService orderCartService;

    @RequestMapping("/doSaveOrder")
    public int saveOrder(String map){
        return 0;
    }

    @RequestMapping("/doSaveOrderTable")
    public int saveOrderTable(Integer orderId, List<Long> orderCartIds){
        orderTableService.saveOrderTable(orderId, orderCartIds);
        return 0;
    }

    @ResponseBody
    @RequestMapping("/doFindOrderByOrderId")
    public SysResult findOrderByOrderId(String orderId, HttpServletRequest request){
        OrderTable orderTable = orderTableService.findByOrderId(Integer.valueOf(orderId));
        Integer productId = orderTable.getProductId();
        String ticket = CookieUtil.getCookieValue(request, PQ_TICKET);
        String json = jedis.get(ticket);
        User user = ObjectMapperUtil.toObject(json, User.class);
        int userId = user.getUserId();
        List<OrderCart> list = orderCartService.findOrderCartByProductId(userId, productId);
        return SysResult.success(list);
    }

    @RequestMapping("/doUpdateOrderByOrderId")
    public int updateOrderByOrderId(Integer siteId, Integer userId, Integer orderId){
        orderTableService.updateOrderByOrderId(siteId, userId, orderId);
        return 0;
    }

    @RequestMapping("/doSaveOrderPay")
    public int saveOrderPay(Integer orderId){
        orderTableService.saveOrderPay(orderId);
        return 0;
    }
}
